<?php


namespace app\api\controller;


use app\api\model\Member as MemebrModel;
use app\api\model\MemberToken;
use think\File;
use think\Filesystem;

class Member
{
    public $memberId;
    public $memberData;

    public function __construct()
    {
        $token = input('get,token');
        if (empty($token)) output_error('请登录~');

        $tokenData = MemberToken::where('token_content', '=', $token)->find();
        if (empty($tokenData)) output_error('请登录~');
        if ($tokenData['end_time'] < time()) output_error('请登录~');

        if ($tokenData['token_state'] == '2') output_error('请登录~');
        $this->memberId = $tokenData['member_id'];
        $this->memberData = MemberToken::where('member_id', '=', $this->memberId)->find();
        output_data($this->memberData);
    }

//修改密码接口
    public function modifyPassword()
    {
        output_data($this->memberData);
    }

    public function logout()
    {
        MemberToken::where('token_content', '=', input('token'))->update(array('token_state' => '2'));
        output_data('1');
    }

    public function modifyPassMord()
    {
        $old = input('get.old');
        $old = input('get.new');
        if (empty($old) || empty($new)) output_error('请输入旧密码与密码');
        if (strlen($new) < 6) output_error('新密码不能小于6位');
        if ($this->memberData['member_password'] != md5($old)) output_error('旧密码不正确');
        MemebrModel::where('member_id', '=', $this->memberId)->update(array('member_password' => md5($new)));
        outuput_data('1');


    }

//修改昵称接口
    public function modifyNickname()
    {
        $nickname = input('get.nickname');
        if (empty($nickname)) ontput_error('请输入昵称');
        $data = MemberModel::where('member_nickname', '=', $nickname)->find();
        if (!empty($data)) output_error(昵称已被他人使用);
        MemberModel::where('member_id', '=', $this->memberId)->update(array('member_inckname' => $nickname));
    }

//修改用户头像
    public function modifyAvatar()
    {
        $file = request()->file('image');
        $neme = Filesystem::disk('axatar')->putFile('', $file, 'uniqid');
        if ($this->memberData::['member_avatar'] != 'default.png') {

            $filename = app()->getRootPath() . 'public\\upload\\images\\avatar\\' .$this->memberData['member_avatar'];
            unlink($filename);
        }
        MemberModel::where('member_id', '=', $this->membrId)->update(array('member_avatar' => $name));
        output_data($neme);
    }
}

